import { saveAs } from 'file-saver';
import { useEffect } from 'react';

export default () => {
  useEffect(() => {
    const base64ToBlob = (code: string) => {
      const parts = code.split(';base64,');
      const contentType = parts[0].split(':')[1];
      const raw = window.atob(parts[1]);
      const rawLength = raw.length;

      const uInt8Array = new Uint8Array(rawLength);

      for (let i = 0; i < rawLength; i += 1) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      return new Blob([uInt8Array], { type: contentType });
    };
    const res = base64ToBlob(
      '',
    );
    const urls = URL.createObjectURL(res);
    console.log(111111111, urls, res);
    // @ts-ignore
    document.getElementById('imgsss').src = urls;
    // saveAs(urls, 'xxx.png');

    const aLink = document.createElement('a');

    const evt = document.createEvent('HTMLEvents');
    evt.initEvent('click', true, true);
    aLink.download = 'xxxxss.png';
    aLink.href = URL.createObjectURL(res);
    aLink.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
  });
  function ScrollTo() {
    const scrollTopTimer = setInterval(function () {
      const top = document.body.scrollTop || document.documentElement.scrollTop;
      const speed = top / 30;
      document.documentElement.scrollTop -= speed;
      if (top == 0) {
        clearInterval(scrollTopTimer);
      }
    }, 5);
  }
  return (
    <div>
      <img id={'imgsss'} src="" alt="" />
      <div style={{ position: 'relative', height: 5000, width: '100%' }}>
        <div onClick={ScrollTo} style={{ position: 'absolute', right: 10, bottom: 100 }}>
          +++
        </div>
      </div>
    </div>
  );
};
